package core

import (
	"fmt"
	"os"
	"strings"

	"gitee.com/krio/ginc/example/core/lib/conf"
	"gitee.com/krio/ginc/example/util/cmdprint"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

const (
	// (***注意：这里要和脚手架工具里的设置成同名称,不要修改***)
	baseFileName = "/apidocs.swagger.json" // swagger首页
)

// SetSwagger 初始化swagger文档
func (engine ApiEngine) SetSwagger() {
	// 是否开启swagger
	if conf.C.Doc != nil && conf.C.Doc.Enable {
		docFileName := "/" + strings.TrimLeft(baseFileName, "/")
		swaggerFilePath := fmt.Sprintf("%s%s", conf.C.Doc.Dir, docFileName)
		if _, err := os.Stat(swaggerFilePath); os.IsNotExist(err) {
			cmdprint.CmdFatal("接口文档错误：%+v", err)
		}
		engine.StaticFile(docFileName, swaggerFilePath)
		engine.GET("/doc/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL(docFileName)))
	}
}
